package com.caiheng.api.service.estate.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caiheng.api.dao.estate.EstateChatApplyMapper;
import com.caiheng.api.dao.estate.EstateChatImgMapper;
import com.caiheng.api.dao.estate.EstateChatMapper;
import com.caiheng.api.dao.estate.EstateMapper;
import com.caiheng.api.dao.wxuser.UserMapper;
import com.caiheng.api.entity.estate.EstateChat;
import com.caiheng.api.entity.estate.EstateChatApply;
import com.caiheng.api.entity.estate.EstateChatImg;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.service.estate.EstateChatService;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.util.DateUtil;
import com.vdurmont.emoji.EmojiParser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 物业经理接待信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-16
 */
@Service
public class EstateChatServiceImpl extends BaseServiceImpl<EstateChatMapper, EstateChat> implements EstateChatService {

    @Resource
    private EstateChatMapper chatMapper;
    @Resource
    private EstateChatImgMapper chatImgMapper;
    @Resource
    private EstateChatApplyMapper chatApplyMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private EstateMapper estateMapper;

    @Value("${web.imgPrefix}")
    private String imgPrefix;

    @Override
    public EstateChat getChatById(int chatId) {
        EstateChat chat = chatMapper.selectById(chatId);
        if(chat != null){
            chat = getChatByModel(chat);
        }
        return chat;
    }

    private EstateChat getChatByModel(EstateChat chat){
        QueryWrapper<EstateChatImg> qwImg = new QueryWrapper<>();
        qwImg.eq("chat_id",chat.getId());
        qwImg.eq("img_type",0);
        List<EstateChatImg> imgs = chatImgMapper.selectList(qwImg);
        if(CollUtil.isNotEmpty(imgs)){
            for (EstateChatImg img : imgs){
                if(StrUtil.isNotEmpty(img.getImgUrl())){
                    img.setImgUrl(imgPrefix + img.getImgUrl());
                }
            }
            chat.setChatImgs(imgs);
        }

        User user = userMapper.selectById(chat.getUserId());
        if(user!=null){
            chat.setFaceUrl(user.getFaceUrl());
            chat.setNickName(user.getNickName());
        }

        QueryWrapper<EstateChatApply> qwRecord = new QueryWrapper<>();
        qwRecord.eq("chat_id", chat.getId());
        qwRecord.orderByAsc("create_time");
        List<EstateChatApply> applyList = chatApplyMapper.selectList(qwRecord);
        if(CollUtil.isNotEmpty(applyList)){
            for (EstateChatApply apply : applyList){
                User user1 = userMapper.selectById(apply.getUserId());
                if(user1!=null){
                    if(StrUtil.isNotEmpty(user1.getNickName())){
                        apply.setNickName(EmojiParser.parseToUnicode(user1.getNickName()));
                    }
                    apply.setFaceUrl(user1.getFaceUrl());
                }


                apply.setTimeStr(DateUtil.getDateStr(apply.getCreateTime()));

                QueryWrapper<EstateChatImg> qwRecordImg = new QueryWrapper<>();
                qwRecordImg.eq("chat_id",chat.getId());
                qwRecordImg.eq("img_type",1);
                List<EstateChatImg> recordImgs = chatImgMapper.selectList(qwRecordImg);
                if(CollUtil.isNotEmpty(recordImgs)){
                    for (EstateChatImg img : recordImgs){
                        if(StrUtil.isNotEmpty(img.getImgUrl())){
                            img.setImgUrl(imgPrefix + img.getImgUrl());
                        }
                    }
                    apply.setChatImgs(recordImgs);
                }
            }
            chat.setChatApplies(applyList);
        }
        return chat;
    }

    @Override
    public List<EstateChat> getListByEstateId(int estateId, int state, int pageIndex, int pageSize) {
        QueryWrapper<EstateChat> qw = new QueryWrapper<>();
        qw.eq("estate_id",estateId);
        qw.eq("state",state);
        qw.orderByDesc("create_time");
        List<EstateChat> list = chatMapper.getPageEntityList(qw,pageIndex,pageSize);
        if(CollUtil.isNotEmpty(list)){
            for (EstateChat chat : list){
                chat = getChatByModel(chat);
            }
        }
        return list;
    }

    @Override
    public List<EstateChat> getListByUserId(int userId, int pageIndex, int pageSize) {
        QueryWrapper<EstateChat> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
//        qw.eq("state",state);
        qw.orderByDesc("create_time");
        List<EstateChat> list = chatMapper.getPageEntityList(qw,pageIndex,pageSize);
        if(CollUtil.isNotEmpty(list)){
            for (EstateChat chat : list){
                chat = getChatByModel(chat);
            }
        }
        return list;
    }
}
